<template>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        label="ID"
        width="50">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="标题"
        width="500">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.title }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="缩略图"
        width="200">
        <template slot-scope="scope">
          <img :src="$fixImgUrl(scope.row.cover[0].url)" alt="" class="thumbnail">
        </template>
      </el-table-column>

      <el-table-column
        label="作者"
        width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.user.nickname }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      @size-change="handleSizeChange"
      :page-sizes="[2, 5, 8,10]" 
      layout="total,sizes,prev, pager, next"
      :total="100"
      @current-change="changePage">
    </el-pagination>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        tableData: [],
        // pageIndex:'',
        pageSize:''
      }
    },
    mounted(){
      this.getPostList(1)
    },
    methods: {
      handleEdit(index, row) {
        console.log(index, row);
        this.$router.push({
          path:'/editPost',
          query:{id:row.id}
        })
      },
      getPostList(pageIndex){
          this.$axios({
          url:'/post',
          method:'get',
          params:{
            pageIndex,
            pageSize:this.pageSize
          }
        }).then(res=>{
          const {data}=res.data
          this.tableData=data;
          // console.log(this.tableData);
        })
      },
      changePage(pageIndex){
        // this.pageIndex=pageIndex;
        this.getPostList(pageIndex)
      },
      handleSizeChange(pageSize){
        this.pageSize=pageSize
        this.getPostList()
      }
    }
  }
</script>

<style lang="less" scoped>
  .thumbnail{
    width: 200px;
    height: 120px;
    object-fit: cover
  }
</style>